<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>


    <script type="text/javascript">
        function getPrice() {
            var x1 = document.getElementById("unitPrice").value;
            var x2 = document.getElementById("storageNum").value;
            document.getElementById("price").value = x1*x2;
        }

        function getSupplierTel() {
            var name = $("#supplierName").val();
            $.ajax({
                url:"/warehousing/getSupplierTel",
                type:"get",
                data:{"name":name},
                dataType:"json",
                success:function (date) {
                    document.getElementById("supplierTel").value = date;
                }
            })
        }

        function getStorageManager() {
            var name = $("#storageName").val();
            $.ajax({
                url:"/warehousing/getStorageManager",
                type:"get",
                data:{"name":name},
                dataType:"json",
                success:function (date) {
                    document.getElementById("storageManager").value = date;
                }
            })
        }
    </script>
</head>
<body>
<div class="x-body">
    <form class="layui-form" id="PurchaseWarehousingForm">

        <div class="layui-form-item">
            <label for="productName" class="layui-form-label">产品名称</label>
            <div class="layui-input-inline">
                <input type="text" id="productName"  name="productName" required="" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label  class="layui-form-label">供应商名称</label>
            <div class="layui-input-inline">
                <select id="supplierName" style="width: 150px;height: 25px" name="supplierName">
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="supplierTel" class="layui-form-label">供应商电话</label>
            <div class="layui-input-inline">
                <input type="text" id="supplierTel" name="supplierTel" required=""
                       autocomplete="off" class="layui-input" oninput="getSupplierTel()">
            </div>
        </div>

        <div class="layui-form-item">
            <label  class="layui-form-label">采购员</label>
            <div class="layui-input-inline">
                <select id="buyer" style="width: 150px;height: 25px" name="buyer">
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="storageDate" class="layui-form-label">入库日期</label>
            <div class="layui-input-inline">
                <input id="storageDate" name="storageDate" required="" placeholder="入库日期" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="productionCompany" class="layui-form-label">生产单位</label>
            <div class="layui-input-inline">
                <input type="text" id="productionCompany" name="productionCompany" required="" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="unitPrice" class="layui-form-label">单价</label>
            <div class="layui-input-inline">
                <input type="text" id="unitPrice"  name="unitPrice" required="" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="storageNum" class="layui-form-label">入库数量</label>
            <div class="layui-input-inline">
                <input type="text" id="storageNum" name="storageNum" required="" lay-verify="storageNum" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="unit" class="layui-form-label">单位</label>
            <div class="layui-input-inline">
                <input type="text" id="unit"  name="unit" required="" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="price" class="layui-form-label">金额</label>
            <div class="layui-input-inline">
                <input type="text" id="price" name="price" required=""
                       autocomplete="off" class="layui-input"  onkeyup="javascript:getPrice()">
            </div>
        </div>

        <div class="layui-form-item">
            <label  class="layui-form-label">仓库名称</label>
            <div class="layui-input-inline">
                <select id="storageName" style="width: 150px;height: 25px" name="storageName">
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="storageManager" class="layui-form-label">仓库管理员</label>
            <div class="layui-input-inline">
                <input type="text" id="storageManager" name="storageManager" required=""
                       autocomplete="off" class="layui-input"  oninput="getStorageManager()">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="remarks" class="layui-form-label">备注</label>
            <div class="layui-input-inline">
                <input type="text" id="remarks" name="remarks" required="" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label  class="layui-form-label">
            </label>
            <button  class="layui-btn" lay-filter="add" lay-submit="">
                添加
            </button>
        </div>
        <span id="info" style="color:red"></span>
    </form>
</div>
<script>
    //定义文档加载函数
    $(function(){
        //加载供应商的名字
        loadSupplier();
        //加载采购员
        loadBuyer();
        //加载仓库名称
        loadStorage();
    })


    layui.use('laydate', function () {
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#storageDate' //指定元素
        });
    });

    //加载供应商
    function loadSupplier() {
        $.ajax({
            url:"/warehousing/supplier",
            async: false,
            type:"get",
            dataType:"json",
            success:function (data) {
                //创建一个拼接字符串的变量
                var html = "";
                html += "<option value=\"--请选择--\">--请选择--</option>";
                //遍历服务端传来的数据集合，就是json数据
                for (var i = 0 ; i < data.length ; i++){
                    html += "<option value='"+data[i].name+"'>"+data[i].name+"</option>";
                }
                //把html加载到下拉框
                $("#supplierName").html(html);
            }
        })
    }

    //加载采购员
    function loadBuyer() {
        $.ajax({
            url:"/warehousing/buyer",
            async: false,
            type:"get",
            dataType:"json",
            success:function (data) {
                //创建一个拼接字符串的变量
                var html = "";
                html += "<option value=\"--请选择--\">--请选择--</option>";
                //遍历服务端传来的数据集合，就是json数据
                for (var i = 0 ; i < data.length ; i++){
                    html += "<option value='"+data[i].name+"'>"+data[i].name+"</option>";
                }
                //把html加载到下拉框
                $("#buyer").html(html);
            }
        })
    }

    //加载仓库名字
    function loadStorage() {
        $.ajax({
            url:"/warehousing/storage",
            async: false,
            type:"get",
            dataType:"json",
            success:function (data) {
                //创建一个拼接字符串的变量
                var html = "";
                html += "<option value=\"--请选择--\">--请选择--</option>";
                //遍历服务端传来的数据集合，就是json数据
                for (var i = 0 ; i < data.length ; i++){
                    html += "<option value='"+data[i].name+"'>"+data[i].name+"</option>";
                }
                //把html加载到下拉框
                $("#storageName").html(html);
            }
        })
    }

    function storageNumNow(){
        var value=$("#storageNum").val();
        var result = 0;
        $.ajax({
            url:"/warehousing/storageNumNow",
            async: false,
            type:"get",
            data:{"storageNum":value},
            dataType:"json",//返回的数据类型
            success:function(data){
                //如果修改成功就跳转到登录页面
                if( data.info-value<10){
                    result = 2;
                }else if(data.info < value){
                    result = 1;
                }
            }
        })
        if(result == 1){
            return '仓库库存已满';
        }else if(result == 2){
            return '仓库库存接近预警值';
        }
        return 0;
    }

    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
            ,layer = layui.layer;

        //自定义验证规则
        form.verify({
            storageNum: function(value) {
                //创建一个正则表达式对象
                var re = /^[0-9]*$/;
                //验证用户输入的入库数量是否是数字
                if (!re.test(value)) {
                    return '入库数量必须为数字！';
                }
                if(storageNumNow() == 1){
                    return '仓库库存已满'
                }
                if(storageNumNow() == 2){
                    return '仓库库存已满'
                }
            }
        });

        //添加采购入库的记录
        form.on('submit(add)', function(data){
            $.ajax({
                url:"/warehousing/insertPurchaseAjax",
                async: false,
                type:"post",
                data:data.field,
                dataType:"json",//返回的数据类型
                success:function(data){
                    //定义{info:"添加成功"}
                    $("#info").text(data);
                    //如果修改成功就跳转到登录页面
                    if(data.info == "添加成功"){
                        layer.alert("添加成功",function(index) {
                            //关闭弹窗操作
                            //获取Frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭弹窗
                            parent.layer.close(index);
                            //刷新
                            parent.window.location.href = parent.window.location.href;
                        });
                    }else{
                        layer.alert("添加失败");
                    }
                }
            })
            return false;
        });
    });

</script>
<script>var _hmt = _hmt || []; (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
</body>
</html>
</body>
</html>